package ms.unicorn.util;

import java.lang.reflect.Field;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.swt.graphics.Color;

public class ConfigUtil {

	public static String get(IConfigurationElement config, String attribName,
			String defaultValue) {
		String value = config.getAttribute(attribName);
		return value != null ? value : defaultValue;
	}

	public static boolean getBoolean(IConfigurationElement config,
			String name, boolean b) {
		return TypeConvert.toBool(get(config, name, Boolean.toString(b)));
	}

	public static int getInt(IConfigurationElement config, String name, int defaultValue) {
		String value = config.getAttribute(name);
		return value != null ? TypeConvert.toInt(value, defaultValue): defaultValue;
	}

	public static int getInt(IConfigurationElement config, String name,
			int defaultValue, Map<String, Integer> str2intMap) {
		String value = config.getAttribute(name);
		return value != null && str2intMap.containsKey(value) ? str2intMap.get(value) : defaultValue;
	}

	public static Color getColor(IConfigurationElement item, String name,
			Color defaultValue) {
		try {
			Field field = ColorConstants.class.getField(item.getAttribute(name));
			return (Color) field.get(null);
		} catch (Exception e) {
			return defaultValue;
		} 
	}
	
	
}
